using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CustomFieldTypes
{
    [CLSCompliant(false)]
    [Guid("187dd74c-fc95-4306-a97c-2f3018c284d6")]
    public class CustomMultiChoiceFieldControl : BaseFieldControl
    {
        protected CheckBoxList checkBoxList1;
        protected TableCell topLeftCell;
        protected TableCell topRightCell;
        protected override string DefaultTemplateName
        {
            get
            {
                return "CustomMultiChoiceFieldControl";
            }
        }
        protected override void CreateChildControls()
        {
            if (SPControlMode.Display != ControlMode)
            {
                base.CreateChildControls();
                checkBoxList1 = (CheckBoxList)TemplateContainer.FindControl("CheckBoxList1");
                topLeftCell = (TableCell)TemplateContainer.FindControl("TopLeftCell");
                topRightCell = (TableCell)TemplateContainer.FindControl("TopRightCell");
                if (!Page.IsPostBack)
                {
                    if (null != Field && null != Field.GetCustomProperty("Values"))
                    {
                        string valuesField = Field.GetCustomProperty("Values").ToString();
                        string[] values = valuesField.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                        StringBuilder script1 = new StringBuilder();
                        StringBuilder script2 = new StringBuilder();
                        script1.Append("<script type=\"text/javascript\">");
                        script1.Append("function " + checkBoxList1.ClientID + "_CheckAll() {");
                        script2.Append("<script type=\"text/javascript\">");
                        script2.Append("function " + checkBoxList1.ClientID + "_UncheckAll() {");
                        int i = 0;
                        foreach (string s in values)
                            if (!String.IsNullOrEmpty(s.Trim()))
                            {
                                checkBoxList1.Items.Add(s);
                                script1.Append("document.getElementById(\"" + checkBoxList1.ClientID +
                                    "_" + i.ToString() + "\").setAttribute(\"checked\",\"checked\");");
                                script2.Append("document.getElementById(\"" + checkBoxList1.ClientID +
                                    "_" + i.ToString() + "\").setAttribute(\"checked\",\"\");");
                                ++i;
                            }
                        script1.Append("} </script>");
                        script2.Append("} </script>");
                        Page.ClientScript.RegisterClientScriptBlock(typeof(CustomMultiChoiceFieldControl), checkBoxList1.ClientID + "_CheckAll()", script1.ToString());
                        Page.ClientScript.RegisterClientScriptBlock(typeof(CustomMultiChoiceFieldControl), checkBoxList1.ClientID + "_UnCheckAll()", script2.ToString());
                        topLeftCell.Text = "<input type=\"button\" onclick=\"" + checkBoxList1.ClientID + "_CheckAll()\" value=\"Check All\" />";
                        topRightCell.Text = "<input type=\"button\" onclick=\"" + checkBoxList1.ClientID + "_UncheckAll()\" value=\"Uncheck All\" />";
                    }
                    if (ControlMode == SPControlMode.New)
                    {
                    }
                }
                else
                {
                    if (null != Field && null != Field.GetCustomProperty("Height"))
                        checkBoxList1.Height = new Unit(int.Parse(Field.GetCustomProperty("Height").ToString()));
                }
            }
        }
        public override object Value
        {
            get
            {
                EnsureChildControls();
                SPFieldMultiChoiceValue v = new SPFieldMultiChoiceValue();
                if (null != checkBoxList1)
                    foreach (ListItem i in checkBoxList1.Items)
                        if (i.Selected) v.Add(i.Text.Trim());
                return v;
            }
            set
            {
                EnsureChildControls();
                SPFieldMultiChoiceValue v = value as SPFieldMultiChoiceValue;
                if (null != v && null != checkBoxList1)
                    for (int i = 0; i < v.Count; ++i)
                        foreach (ListItem li in checkBoxList1.Items)
                            if (v[i].Trim() == li.Text.Trim())
                            {
                                li.Selected = true;
                                break;
                            }
                base.Value = value;
            }
        }
    }
}
